package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/admin/category")
@RestController
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     */
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("要添加的菜品信息：{}", categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 分类分页查询
     */
    @GetMapping("/page")
    Result getPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("条件分页查询的类容：{}",categoryPageQueryDTO);
        PageResult pageResult=categoryService.getPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改status
     */
    @PostMapping("/status/{status}")
    Result changeStatus(@PathVariable Integer status,Long id){
        log.info("要修改的员工id:{},status:{}",id,status);
        categoryService.changeStatus(status,id);
        return Result.success();
    }

    /**
     * 修改分类
     */
    @PutMapping
    Result changeInfo(@RequestBody CategoryDTO categoryDTO){
        log.info("修改后的信息：{}",categoryDTO);
        categoryService.changeInfo(categoryDTO);
        return Result.success();
    }

    /**
     * 根据id删除分类
     */
    @DeleteMapping
    Result delete(Long id){
        log.info("要删除的员工id：{}",id);
        categoryService.delete(id);
        return Result.success();
    }

    /**
     * 根据类型查询分类
     */
    @GetMapping("/list")
    Result getByType(Integer type){
        log.info("type:{}",type);
        List<Category> category =categoryService.getByType(type);
        return Result.success(category);
    }

}
